<?php

/**
 * @author Immanuel Bauer | 0626117
 */
class MessageJSON {
	
	/**
	 * Generates the JSON view for the given Message model object or array of Message model object.
	 * @param Message $msg
	 * @param boolean $fullResponse
	 * @param int $count
	 * @return string
	 */
	public static function getView($msg, $fullResponse = false, $count = null, $order = 'asc'){
		
		if(is_array($msg)){
			$key = 'msgs';
			$mc = count($msg);
			$a = array();
			
			for($c = 0; $c < $mc; $c++){
				$m = $msg[$c];
				$ma = MessageJSON::getFullMsgArray($m);
					
				$responses = Message::getReplys($m->getID(), $order);
				
				if(!is_null($responses)){
					$ar = array();
					$rc = count($responses);
					for($r = 0; $r < $rc; $r++){
						array_push($ar, MessageJSON::getMsgArray($responses[$r], $fullResponse, $order));
					}
					if($rc > 0) $ma['r'] = $ar;
				}
				
				array_push($a, $ma);
			}
		} else {
			$key = 'msg';
			$a = MessageJSON::getFullMsgArray($msg);
				
			$responses = Message::getReplys($msg->getID(), $order);
			
			if(!is_null($responses)){
				$ar = array();
				$rc = count($responses);
				for($r = 0; $r < $rc; $r++){
					array_push($ar, MessageJSON::getMsgArray($responses[$r], $fullResponse, $order));
				}
				if($rc > 0) $a['r'] = $ar;
			}
		}
		
		$res = new ResultJSON(false, session_id());
		$res->addItem($key, $a);
		if(!is_null($count)) $res->addItem('mC', $count);
		
		return $res->renderView();
	}
	
	/**
	 * Creates an array of the given Message and its responses
	 * @param Message $msg
	 * @param boolean $fullResponse
	 */
	private static function getMsgArray($msg, $fullResponse, $order){
		if($fullResponse){
			$a = MessageJSON::getFullMsgArray($msg);
				
		} else {
			$a = MessageJSON::getLiteMsgArray($msg);
		}
		
		$responses = Message::getReplys($msg->getID(), $order);
		
		if(!is_null($responses)){
			$ar = array();
			$rc = count($responses);
			for($r = 0; $r < $rc; $r++){
				array_push($ar, MessageJSON::getMsgArray($responses[$r], $fullResponse, $order));
			}
			if($rc > 0) $a['r'] = $ar;
		}
		
		return $a;
	}
	
	/**
	 * Creates a full array of the given Message
	 * @param Message $msg
	 */
	private static function getFullMsgArray($msg){
		$author = User::getUser($msg->getAuthorID());
		$a = array(
				'id' => $msg->getID(),
				'title' => $msg->getTitle(),
				'text' => $msg->getText(),
				'date' => $msg->getDate(),
				'author' => array(
					'id' => $author->getID(),
					'nick' => $author->getLoginName(),
					'fN' => $author->getFirstName(),
					'lN' => $author->getLastName())
			);
		
		if($msg->getMessageID() != '') $a['mID'] = $msg->getMessageID();
		else if ($msg->getTodoID() != '') $a['tID'] = $msg->getTodoID();
		else if ($msg->getListID() != '') $a['lID'] = $msg->getListID();
		else if ($msg->getUserID() != '') $a['uID'] = $msg->getUserID();
		
		return $a;
	}
	
	/**
	 * Creates a lite array of the given Message containing only the id
	 * @param Message $msg
	 */
	private static function getLiteMsgArray($msg){
		$a = array('id' => $msg->getID());
		
		return $a;
	}
	
}

?>